package com.qingke.oms.command.shop;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.Goods;
import com.qingke.oms.pojo.GoodsStatistic;
import com.qingke.oms.pojo.Order;
import com.qingke.oms.pojo.OrderStatus;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class ShopStatusCommand extends ShopCommand implements UpdateGoodsStatistic{

	public ShopStatusCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		if(user.getId()!=shop.getOwner().getId()){
			OMSConsole.println("It's not your shop,you can't look shop status.");
			return ;
		}
		
		String cmd;
		while(true){
			cmd=OMSConsole.prompt("status>");
			if("statistic".equalsIgnoreCase(cmd)){
				statistic();
				OMSConsole.println();
			}
			else if("order".equalsIgnoreCase(cmd)){
				order();
				OMSConsole.println();
			}
			else if("shop".equalsIgnoreCase(cmd)){
				shop();
				OMSConsole.println();
			}
			else if("customer".equalsIgnoreCase(cmd)){
				customer();
				OMSConsole.println();
			}
			else if("help".equalsIgnoreCase(cmd)){
				help();
				OMSConsole.println();
			}
			else if("exit".equalsIgnoreCase(cmd)){
				break;
			}
			else{
				OMSConsole.println("err input.");
			}
		}
	}
	
	private void statistic(){
		updateGoodsStatistic();
		shop=dao.selectNewestShopById(shop.getId());
		Set<GoodsStatistic> list=shop.getGoodsStatistics();
		OMSConsole.println("-------goods statistic-------");
		for(GoodsStatistic gr:list){
			Goods goods=gr.getGoods();
			OMSConsole.print(goods);
			OMSConsole.println("      [Stock]:"+gr.getStock()+",[price]:"+gr.getPrice());
		}
	}
	
	private void order(){
		String cmd;
		while(true){
			cmd=OMSConsole.prompt("order>");
			boolean pass=false;
			String code=null;
			if("all".equalsIgnoreCase(cmd)){
				pass=true;
				OMSConsole.println("-------all orders------");
			}
			else if("wait pay".equalsIgnoreCase(cmd)){
				code="wait_pay";
				OMSConsole.println("-------wait pay orders------");
			}
			else if("wait delivery".equalsIgnoreCase(cmd)){
				code="wait_delivery";
				OMSConsole.println("-------wait delivery orders------");
			}
			else if("shipped".equalsIgnoreCase(cmd)){
				code="shipped";
				OMSConsole.println("-------shipped orders------");
			}
			else if("sign".equalsIgnoreCase(cmd)){
				code="sign";
				OMSConsole.println("-------wait pay orders------");
				
			}
			else if("help".equalsIgnoreCase(cmd)){
				OMSConsole.println("- all               list all orders");
				OMSConsole.println("- wait pay          list wait pay orders");
				OMSConsole.println("- wait delivery     list wait delivery  orders");
				OMSConsole.println("- shipped           list shipped orders");
				OMSConsole.println("- sign              list sign orders");
				OMSConsole.println("- exit              exit order command");
				OMSConsole.println();
				continue;
			}
			else if("exit".equalsIgnoreCase(cmd)){
				break;
			}
			else{
				OMSConsole.println("err input");
				continue;
			}
			Collection<Order> list;
			if(pass){
				OrderStatus orderStatus=dao.selectOrderStatusByCode(code);
				list=dao.selectOrderByShopAndOrderStatus(shop,orderStatus);
			}
			else{
				list=shop.getOrders();
			}
			for(Order o:list){
				OMSConsole.println(o);
			}
			OMSConsole.println();
		}
	}
	
	private void shop(){
		String str="Shop [id]:"+shop.getId()+",[shop name]:"+shop.getName()+",[owner]:"+user.getName();
		if(!"".equals(shop.getDescription().trim())){
			str+=",[description]:"+shop.getDescription();
		}
		OMSConsole.println(str);
	}
	private void help(){
		OMSConsole.println("- statistic     list goods statistic");
		OMSConsole.println("- order         into order command");
		OMSConsole.println("- shop          list shop property");
		OMSConsole.println("- customer      list shop's customer");
		OMSConsole.println("- help          list all status command");
		OMSConsole.println("- exit          exit status");
	}
	
	private void customer(){
		shop=dao.selectNewestShopById(shop.getId());
		Set<Order> listOrdser = shop.getOrders();
		List<User> listCustomer=new ArrayList<>();
		for(Order o:listOrdser){
			User user=o.getCustomer();
			listCustomer.add(user);
		}
		OMSConsole.println("------customer list-------");
		for(User u:listCustomer){
			OMSConsole.println(u);
		}
	}
	

}
